package com.admin.common.utils;

import com.github.pagehelper.Page;

import java.util.Collections;
import java.util.List;

/**
 * 分页工具
 *
 * @author sq-lan
 */
public class PageUtil extends cn.hutool.core.util.PageUtil {

    /**
     * List 分页
     */
    public static <T> List<T> paging(int page, int size, List<T> list) {
        int fromIndex = page * size;
        int toIndex = page * size + size;
        if (fromIndex > list.size()) {
            return Collections.emptyList();
        } else if (toIndex >= list.size()) {
            return list.subList(fromIndex, list.size());
        } else {
            return list.subList(fromIndex, toIndex);
        }
    }

    public static <T> PageResult<T> toPageHelper(List<T> list) {
        Page<T> page = (Page<T>) (list);
        return PageUtil.toPage(page.getResult(), page.getTotal());
    }

    /**
     * 自定义分页
     */
    public static <T> PageResult<T> toPage(List<T> list) {
        return new PageResult<>(list, list.size());
    }


    /**
     * 自定义分页
     */
    public static <T> PageResult<T> toPage(List<T> list, long totalElements) {
        return new PageResult<>(list, totalElements);
    }
}
